package com.tc.quartz.server.core;

import com.tc.quartz.util.QrtzUtil;
import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.Trigger.CompletedExecutionInstruction;
import org.quartz.TriggerListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 公共监听器
 * @author yongjin.ye
 *
 */
public class GlobalTriggerListener implements TriggerListener {

	protected static Logger log = LoggerFactory.getLogger(GlobalTriggerListener.class);
	
	@Override
	public String getName() {
		return "GlobalTriggerListener";
	}

	@Override
	public void triggerFired(Trigger trigger, JobExecutionContext context) {
		log.info("GlobalTriggerListener->triggerFired. jobname=" + trigger.getJobKey().getName());
		
	}

	@Override
	public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
		log.info("GlobalTriggerListener->vetoJobExecution->false. jobname=" + trigger.getJobKey().getName());
		//如果返回TRUE任务job会被终止
		return false;
	}

	@Override
	public void triggerMisfired(Trigger trigger) {
		log.warn("GlobalTriggerListener->triggerMisfired. (错过触发时间,可能是服务器时间不一致或线程池已满) jobname=" + trigger.getJobKey().getName());
		try {
			IPaymentQuartzService quartzService = QrtzUtil.getBean(IPaymentQuartzService.class);
			quartzService.insertJobRunLogOfTriggerMisfired(trigger);
		} catch (Exception e) {
			log.error("错过触发时间，插入log异常", e);
		}
	}

	@Override
	public void triggerComplete(Trigger trigger, JobExecutionContext context,
			CompletedExecutionInstruction triggerInstructionCode) {
		log.info("GlobalTriggerListener->triggerComplete. jobname=" + trigger.getJobKey().getName());
	}

}
